-------------------------------------------------------------------------------
--  The Legend of Zelda : Oracle of Secrets (MapEditor)
--  Copyright (C) 2010 Pixelua
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------------

editionStart = false
map_selected = false
already_exist = false

j = 0
map = {}
map.cursor = 1
map.tab = {}

tile = {}
tile.cursor = 1
tile.tab = {}

worldmap = {}
if plain_icon == nil then plain_icon = Image.load("map_icon/plain.png", VRAM) end
if worldmap.img == nil then worldmap.img = Image.load("worldmap.png", VRAM) end
worldmap.x = 1
worldmap.y = 1
worldmap.tab = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"}
worldmap.selected = ""

-- File Checker --
function fileChecker()
	files = System.listDirectory("./tiles")
	tile.tab = {}
        
	for i=1, #files do
		if string.lower(string.sub(files[i].name, -3)) == "png" then
			table.insert(tile.tab,string.lower(files[i].name))
		end
	end
        
	files = {}
	files = System.listDirectory("./maps")
	map.tab = {}
        
	for i=1, #files do
		if string.lower(string.sub(files[i].name, -3)) == "map" and files[i].name ~= "empty.map" and files[i].name ~= "worldmap.map" then
			table.insert(map.tab,string.lower(files[i].name))
		end
	end
end

function worldmap_move()
	if Keys.newPress.Down then
		if worldmap.y < 15 then
			worldmap.y = worldmap.y + 1
		end
	elseif Keys.newPress.Up then
		if worldmap.y > 1 then
			worldmap.y = worldmap.y - 1
		end
	elseif Keys.newPress.Right then
		if worldmap.x < 15 then
			worldmap.x = worldmap.x + 1
		end
	elseif Keys.newPress.Left then
		if worldmap.x > 1 then
			worldmap.x = worldmap.x - 1
		end
	end
end

function cursormove(target)
	if Keys.newPress.Down then
		if target.cursor == #target.tab then
			target.cursor = 1
		else
			target.cursor = target.cursor + 1
		end
	elseif Keys.newPress.Up then
		if target.cursor == 1 then
			target.cursor = #target.tab
		else
			target.cursor = target.cursor - 1
		end
	end
end

function menu_draw()
	screen.print(SCREEN_UP, 128-((#("Zelda Map Editor")*6)/2), 84, "Zelda Map Editor")
	screen.print(SCREEN_UP, 128-((#("Par Fantasix")*6)/2), 92, "Par Fantasix")
	screen.print(SCREEN_UP, 9, 140, "Map Editor Controls") 
	screen.print(SCREEN_UP, 8, 140, "Map Editor Controls",Color.new(26,26,26))
	screen.drawLine(SCREEN_UP, 8, 149, 10+#("Map Editor Controls")*6,149, Color.new(26,26,26))
	screen.print(SCREEN_UP, 8, 152, "Pad to choose a Tile")
	screen.print(SCREEN_UP, 8, 160, "Stylus to set the Tile")
	screen.print(SCREEN_UP, 8, 168, "Start to save the Map")
	screen.print(SCREEN_UP, 8, 176, "X to fill the map with the current Tile")
end

function mapfileDone()
	mapfile = worldmap.selected..".map"
	for i=1, #map.tab do 
		if mapfile == map.tab[i] then
			already_exist = true
			break
		end
	end
               
	if not already_exist then
		newfile = io.open("./maps/"..mapfile, "w") 
		io.output(newfile)
		towrite = ""
                       
		for y = 0 , 11 do
			towrite = towrite.."0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|\n"
		end
			io.write (towrite)
		io.close()
		fileChecker()
	end
	map_selected = true
end

fileChecker()
-------------
-- BOUCLES --
-------------
while not map_selected do
	Controls.read()
        
	menu_draw()
	worldmap_move()
	screen.blit(SCREEN_DOWN, 60,30,worldmap.img)
        
	for i=1,#map.tab do
		for j=1, #worldmap.tab do
			if string.sub(map.tab[i],1,1) == worldmap.tab[j] then
				screen.blit(SCREEN_DOWN, 68+((string.sub(map.tab[i],2,-5)-1)*8),31+((j-1)*8),plain_icon)
			end
		end
	end
        
	worldmap.selected = worldmap.tab[worldmap.y]..worldmap.x
	screen.print(SCREEN_DOWN, 8, 8,"Cursor Position : "..worldmap.selected)
        
	screen.drawRect(SCREEN_DOWN,67+((worldmap.x-1)*8),30+((worldmap.y-1)*8),76+((worldmap.x-1)*8),39+((worldmap.y-1)*8),Color.new(31,0,0))
	if Stylus.newPress then
		if Stylus.X >= 68 and Stylus.X <= 187 and Stylus.Y >= 31 and Stylus.Y <= 150 then
			worldmap.x = math.floor((Stylus.X-68)/8)+1
			worldmap.y = math.floor((Stylus.Y-31)/8)+1
			worldmap.selected = worldmap.tab[worldmap.y]..worldmap.x
			mapfileDone()
		end
	end
	
	if Keys.newPress.A then
		mapfileDone()
	end
        
	render()
end

while not editionStart and map_selected do
	Controls.read()
	menu_draw()
        
	screen.print(SCREEN_DOWN, 16, 8, "Map :"..mapfile ,Color.new(31,15,0))
	screen.print(SCREEN_DOWN, 16, 24, "Choose a TileSet" ,Color.new(31,0,0))
        
	for i=1, #tile.tab do
		screen.print(SCREEN_DOWN, 16, 24+(i*8), tile.tab[i])
	end

	cursormove(tile)
	screen.print(SCREEN_DOWN, 8, 24+tile.cursor*8, ">", Color.new(31,0,0))
	map.cursor = 1
        
	if Keys.newPress.A then
		editionStart = true
	end

	render()
end

maptile = string.lower(tile.tab[tile.cursor])
mapfile = string.lower(mapfile)
----- Destroy -----
--Image.destroy(worldmap.img)
--worldmap.img = nil
--Image.destroy(plain_icon)
--plain_icon = nil

worldmap = nil
already_exist = nil
map_selected = nil
editionStart = nil
map = nil
tile = nil
files = nil
collectgarbage()
dofile("zeldamapeditor.lua")
